package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 查询3年级2班的所有学生
 */
public class Test5 {
    public static void main(String[] args) {
        try(
                Connection connection = DBUtil.getConnection();
        ){
            Statement statement = connection.createStatement();
            /*
                SELECT s.name,s.age,c.name
                FROM student s,class c
                WHERE s.class_id=c.id
                AND c.name='3年级2班'
             */
            String sql = "SELECT s.name sn,s.age,c.name cn " +
                         "FROM student s,class c " +
                         "WHERE s.class_id=c.id " +
                         "AND c.name='3年级2班'";
            System.out.println(sql);
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                String sname = rs.getString("sn");
                int sage = rs.getInt("age");
                String cname = rs.getString("cn");
                System.out.println(sname+","+sage+","+cname);
            }
            rs.close();

        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
